package org.sjs.dreamfly.common.util

import com.google.common.reflect.TypeToken
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory

object JsonUtil {

    val moshi = Moshi.Builder()
        .add(KotlinJsonAdapterFactory())
        .build()

    fun toJson(obj: Any) : String {
        return moshi.adapter(obj.javaClass).toJson(obj)
    }

    inline fun <reified T> fromJson(json: String): T? {
        val type = object : TypeToken<T>() {}.type
        return moshi.adapter<T>(type).fromJson(json)
    }
}